package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.SystemButton;
import com.tbm.console.proj.view.SystemButtonMenuView;
import com.tbm.console.proj.repository.SystemButtonRepository;
import com.tbm.console.proj.service.SystemButtonService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SystemButtonServiceImpl implements SystemButtonService {

    @Autowired
    private SystemButtonRepository systemButtonRepository;

    @Override
    public List<SystemButton> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<SystemButton> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<SystemButton> findAll(PageData pageData) {
        Specification<SystemButton> sp = this.getSystemButtonSpecification(pageData);
        return systemButtonRepository.findAll(sp);
    }

    @Override
    public List<SystemButton> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SystemButton> sp = this.getSystemButtonSpecification(pageData);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return systemButtonRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public SystemButton findById(Integer id) {
        Integer[] ids = new Integer[]{id};
        List<SystemButton> list = findAllById(ids);
        if(list.size()==0){
            return new SystemButton();
        }
        return list.get(0);
    }

    @Override
    public List<SystemButton> findAllById(Integer [] ids) {
        return systemButtonRepository.findAllById(Arrays.asList(ids));
    }

    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(SystemButton systemButton) {
        return systemButtonRepository.count(Example.of(systemButton));
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemButton> sp = this.getSystemButtonSpecification(pageData);
        return systemButtonRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        systemButtonRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean deleteByIds(Iterable<SystemButton> ids) {
        return false;
    }


    @Override
    public SystemButton save(SystemButton systemButton) {
        List<SystemButton> list = saveAll(Arrays.asList(systemButton));
        return list.size() == 0 ? new SystemButton() : list.get(0);
    }

    @Override
    public List<SystemButton> saveAll(List<SystemButton> systemButtons) {
        return systemButtonRepository.saveAll(systemButtons);
    }
//    @Override
//    public List<SystemButtonMenuView> findSystemButtonMenuView(Integer start,Integer length) {
//        List<Sort.Order> list = new ArrayList<>();
//        Sort.Order menuNameOrder = new Sort.Order(Sort.Direction.DESC, "menuID");
//        Sort.Order idOrder = new Sort.Order(Sort.Direction.ASC, "id");
//        list.add(menuNameOrder);
//        list.add(idOrder);
//        Sort sort = new Sort(list);
//        Pageable pageable = new PageRequest(start, length, sort);
//        return systemButtonRepository.findSystemButtonMenuView(pageable);
//    }
    @Override
    public List<SystemButton> findSystemButtonLocalQuery(String menuURL) {
        return systemButtonRepository.findSystemButtonLocalQuery(menuURL);
    }
    //通过Specification来获取自定义的查询条件
    private Specification<SystemButton> getSystemButtonSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("---")) {
                    preList.add(criteriaBuilder.greaterThan(root.get("--"), pageData.get("--").toString()));
                }
                if (null != pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                if (null != pageData.get("menuID")) {
                    preList.add(criteriaBuilder.equal(root.get("menuID"), Integer.valueOf(pageData.get("menuID").toString())));
                }
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}
